package com.shawn.db.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.shawn.bean.BlackNumInfo;
import com.shawn.db.BlackNumOpenHlper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by shawn on 2016/4/14.
 */
public class BlackNumDao {

    public static final int CALL=0;
    public static final int SMS=1;
    public static final int ALL=2;

    private BlackNumOpenHlper blackNumOpenHlper;
    public BlackNumDao(Context context){
        blackNumOpenHlper = new BlackNumOpenHlper(context);
    }

    /**
     *  添加黑名单
     * @param blacknum
     * @param mode
     */
    public void addBlackNum(String blacknum,int mode){
        SQLiteDatabase database = blackNumOpenHlper.getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put("blacknum",blacknum);
        values.put("mode",mode);
        database.insert(BlackNumOpenHlper.DB_NAME, null, values);
        database.close();
    }

    public void updateBlackNum(String blacknum,int mode){
        SQLiteDatabase database = blackNumOpenHlper.getReadableDatabase();
        ContentValues values = new ContentValues();
        values.put("mode",mode);
        database.update(BlackNumOpenHlper.DB_NAME, values, "blacknum=?", new String[]{blacknum});
        database.close();
    }

    public void deleteBlackNum(String blacknum){
        SQLiteDatabase database = blackNumOpenHlper.getReadableDatabase();
        database.delete(BlackNumOpenHlper.DB_NAME, "blacknum=?", new String[]{blacknum});
        database.close();
    }

    /**
     * 通过黑名单号码,查询黑名单号码的拦截模式
     */
    public int queryBlackNumMode(String blacknum){
        int mode = -1;
        SQLiteDatabase database = blackNumOpenHlper.getReadableDatabase();
        Cursor cursor = database.query(BlackNumOpenHlper.DB_NAME, new String[]{"mode"}, "blacknum=?",
                new String[]{blacknum}, null, null, null);
        if(cursor.moveToNext()) {
            mode = cursor.getInt(0);
        }
        cursor.close();
        database.close();
        return mode;
    }


    /**
     * 查询全部黑名单号码
     */
    public List<BlackNumInfo> queryAllBlackNum(){
        List<BlackNumInfo> list = new ArrayList<>();
        SQLiteDatabase database = blackNumOpenHlper.getReadableDatabase();
        Cursor cursor = database.query(BlackNumOpenHlper.DB_NAME,new String[]{"blacknum","mode"},
                null,null,null,null,"_id desc");
        while(cursor.moveToNext()) {
            String blacknum = cursor.getString(0);
            int mode = cursor.getInt(1);
            BlackNumInfo blackNumInfo = new BlackNumInfo(blacknum,mode);
            list.add(blackNumInfo);
        }
        cursor.close();
        database.close();
        return list;
    }

    /**
     * 查询部分数据
     * 查询20条数据
     * MaxNum : 查询的总个数
     * startindex : 查询的起始位置
     * @return
     */
    public List<BlackNumInfo> getPartBlackNum(int MaxNum,int startindex){
        List<BlackNumInfo> list = new ArrayList<>();
        SQLiteDatabase database = blackNumOpenHlper.getReadableDatabase();
        Cursor cursor = database.rawQuery("select blacknum,mode from info order by _id desc limit ? offset ?", new String[]{MaxNum+"",startindex+""});
        while(cursor.moveToNext()) {
            String blacknum = cursor.getString(0);
            int mode = cursor.getInt(1);
            BlackNumInfo blackNumInfo = new BlackNumInfo(blacknum,mode);
            list.add(blackNumInfo);
        }
        cursor.close();
        database.close();
        return list;
    }
}
